package jxta.chat;

import jxta.chat.entities.VoiceChatMessage;
import jxta.chat.util.Constants;
import jxta.test.JxtaManager;
import net.jxta.endpoint.Message;

public class MessageHandlerFactory {

	private static MessageHandlerFactory mInstance = null;
	
	
	private MessageHandlerFactory(){
	}
	
	public static MessageHandlerFactory getInstance(){
		if (mInstance == null){
			mInstance = new MessageHandlerFactory();
		}
		
		return mInstance;
	}
	
	public AbstractMessageHandler getMessageHandler(Message msg, JxtaManager chatManager){
		String type = msg.getMessageElement(Constants.MESSAGE_TYPE_SPACE) != null ? msg.getMessageElement(Constants.MESSAGE_TYPE_SPACE).toString() : null;
		AbstractMessageHandler retVal = null;
		
		if (type == null){
			return null;
		}
		
		if (type.equalsIgnoreCase(Constants.MessageTypes.REGULAR.getName())){
			retVal = new RegularMessageHandler();
		}
		else if (type.equalsIgnoreCase(Constants.MessageTypes.LOGIN.getName())){
			retVal = new LoginMessageHandler();
		}
		else if (type.equalsIgnoreCase(Constants.MessageTypes.LOGOUT.getName())){
			retVal = new LogOutMessageHandler();
		}
		else if (type.equalsIgnoreCase(Constants.MessageTypes.VOIP_CHAT.getName())){
			retVal = new VoiceChatMessageHandler(chatManager);
		}
		
		return retVal;
	}
}